﻿using System.Web;
using System.Text;
using System.IO;
using System.Data;
using System;

/// <summary>
/// ListPage 的摘要说明
/// </summary>
public class ListPage:StaticBase
{
    public ListPage()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 是否操作成功
    /// </summary>
    private bool o_sucess = true;
    /// <summary>
    /// 错误信息
    /// </summary>
    private string errorstring = string.Empty;
    /// <summary>
    /// 模板文件名称
    /// </summary>
    private string masterhtml;
    /// <summary>
    /// 数据源 
    /// </summary>
    //private IEnumerable<DataRow> datasheet;
    private DataTable dt;
    /// <summary>
    /// 时间戳
    /// </summary>
    private string name;
    /// <summary>
    /// 时间戳
    /// </summary>
    private string type;
    /// <summary>
    /// 模块类别
    /// </summary>
    private FlagsFileName fname;

    /// <summary>
    /// 输出文件路径
    /// </summary>
    private string serverpath;

    private string tab=null;
    /// <summary>
    /// 构造器静态生成对象
    /// </summary>
    /// <param name="tb">需要生成静态文件的数据源</param>
    /// <param name="fn">文件类别枚举</param>
    /// <param name="time">此字段为数据库表中字段，由该字段指定生成的文件名字标志 </param>
    public ListPage(DataTable dt, FlagsFileName fn)
    {
        this.fname = fn;
        this.dt = dt;
        this.masterhtml = FileName[fn] + ".html";
        WriteFile();
    }
    public ListPage(DataTable dt, FlagsFileName fn,string n)
    {
        this.tab = n;
        this.fname = fn;
        this.dt = dt;
        this.masterhtml = FileName[fn] + ".html";
        WriteFile();
    }
    public override string Errorstring
    {
        get { return errorstring; }
        set { errorstring = value; }
    }

    public override bool Osucess
    {
        get { return o_sucess; }
        set { o_sucess = value; }
    }

    protected override bool WriteFile()
    {
        string str = "";
        try//从指定模板文件中读取html代码
        {
            sr = new StreamReader(HttpContext.Current.Server.MapPath(PagePath + this.fname + '/' + this.masterhtml), code);
            str = sr.ReadToEnd();
        }
        catch (Exception ex)//异常则指定返回的错误信息 
        {
            sr.Close();
            sr.Dispose();
            this.o_sucess = false;
            this.errorstring = ex.Message;
            return this.o_sucess;
        }
        finally
        {
            sr.Close();
            sr.Dispose();
        }
        //要保存的文件名称
        string filepath = string.Empty;
        string newString = string.Empty;
        serverpath = HttpContext.Current.Server.MapPath(SavePath + this.fname + '/');
        try
        {
            StringBuilder sb = new StringBuilder();
            string path = serverpath + this.masterhtml;
            Encoding code = Encoding.GetEncoding("UTF-8");
            switch (this.tab)
            {
                case null:
                    sb.Append("<div id='class-1'>");
                    foreach (DataRow row in dt.Rows)
                    {
                        sb.Append("<li><a href=" + row[1].ToString() + ">" + row[0].ToString() + "</a><li>");
                    }
                    sb.Append("</div>");
                    sb.Append(str);

                    sw = new StreamWriter(path, false, code);
                    sw.Write(sb);
                    sw.Flush();
                    sb.Clear();
                    break;
                case "navigation":
                    sb.Append("<ul class='nav navbar-nav' id='navigation-1'>");
                    foreach (DataRow row in dt.Rows)
                    {
                        //sb.Append("<li><a href=" + row[1].ToString() + ">" + row[0].ToString() + "</a><li>");
                        sb.Append("<li><a href='#'>" + row[0].ToString() + "</a><li>");
                    }
                    sb.Append("</ul>");
                    sb.Append(str);

                    sw = new StreamWriter(path, false, code);
                    sw.Write(sb);
                    sw.Flush();
                    sb.Clear();
                    break;
                default: break;
            }
           

                
        }
        catch (Exception ex)
        {
            this.o_sucess = false;
            this.errorstring = ex.Message;
            return this.o_sucess;
        }
        finally { sw.Close(); }
        sw.Dispose();
        return true;
    }

}